import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:my_desk_flutter/controller/bus_controller.dart';
import 'package:my_desk_flutter/extions/rivepod_ext.dart';
import 'package:my_desk_flutter/model/bus_info_model.dart';
import 'package:my_desk_flutter/widget/auth_check.dart';

import '../widget/bus_list_card.dart';

class BusSearchPage extends StatefulWidget {
  const BusSearchPage({super.key});

  @override
  State<BusSearchPage> createState() => _BusSearchPageState();
}

class _BusSearchPageState extends State<BusSearchPage> {
  late final TextEditingController textEditingController =
      TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.all(15.w),
              child: Row(
                children: [
                  Expanded(
                    child: TextField(
                      controller: textEditingController,
                      decoration: const InputDecoration(
                        hintText: '输入线路',
                        border: InputBorder.none,
                      ),
                    ),
                  ),
                  Consumer(
                    builder:
                        (BuildContext context, WidgetRef ref, Widget? child) {
                      return Row(
                        children: [child!],
                      );
                    },
                    child: AuthCheck(
                      onTap: () {
                        context
                            .read(busControllerProvider.notifier)
                            .searchByName(textEditingController.text);
                      },
                      child: ElevatedButton(
                        onPressed: () {},
                        child: const Text("搜索"),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Consumer(
                builder: (BuildContext context, WidgetRef ref, Widget? child) {
                  final data = ref.watch(busControllerProvider);
                  return switch (data) {
                    AsyncData<List<BusInfoModel>>(:final value) =>
                      _buildBusList(value),
                    AsyncError() => const Text("发生了错误"),
                    AsyncLoading() => const CupertinoActivityIndicator(),
                    _ => const Text("asldasd"),
                  };
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBusList(List<BusInfoModel> value) {
    return ListView.builder(
      itemBuilder: (_, index) {
        final data = value[index];
        return BusListCard(
          data: data,
        );
      },
      itemCount: value.length,
    );
  }
}
